package com.butioy.study.jersey.utils;

import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author yanglq
 * @since 3.0.0
 */
public class MybatisUtils {

    private static final SqlSessionFactory sqlSessionFactory;

    private static final String resource = "mybatis-config.xml";

    static {
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e) {
            throw new RuntimeException("初始化mybatis配置异常", e);
        }
    }

    public static SqlSession getSession() {
        return sqlSessionFactory.openSession();
    }

}
